package com.cyy.springbootinit.service.impl;
import java.util.Date;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyy.springbootinit.common.ErrorCode;
import com.cyy.springbootinit.constant.CommonConstant;
import com.cyy.springbootinit.exception.ThrowUtils;
import com.cyy.springbootinit.mapper.DormitoryExpenseBillMapper;
import com.cyy.springbootinit.model.dto.dorimitoryexpensebill.DormitoryExpenseBillQueryRequest;
import com.cyy.springbootinit.model.entity.Dormitory;
import com.cyy.springbootinit.model.entity.DormitoryExpenseBill;
import com.cyy.springbootinit.model.entity.User;
import com.cyy.springbootinit.model.vo.DormitoryExpenseBillVO;
import com.cyy.springbootinit.model.vo.DormitoryVO;
import com.cyy.springbootinit.model.vo.UserVO;
import com.cyy.springbootinit.service.DormitoryExpenseBillService;
import com.cyy.springbootinit.service.DormitoryService;
import com.cyy.springbootinit.service.UserService;
import com.cyy.springbootinit.utils.SqlUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 宿舍水电支出服务实现
 *
 * @author 程序员cyy</a>
 * @from cyy</a>
 */
@Service
@Slf4j
public class DormitoryExpenseBillServiceImpl extends ServiceImpl<DormitoryExpenseBillMapper, DormitoryExpenseBill> implements DormitoryExpenseBillService {

    @Resource
    private DormitoryExpenseBillMapper dormitoryExpenseBillMapper;
    @Resource
    private UserService userService;
    @Resource
    private DormitoryService dormitoryService;

    /**
     * 校验数据
     *
     * @param dormitoryExpenseBill
     * @param add      对创建的数据进行校验
     */
    @Override
    public void validDormitoryExpenseBill(DormitoryExpenseBill dormitoryExpenseBill, boolean add) {


        ThrowUtils.throwIf(dormitoryExpenseBill == null, ErrorCode.PARAMS_ERROR);
        // todo 从对象中取值
        Integer dormitoryId = dormitoryExpenseBill.getDormitoryId();
        Double waterExpense = dormitoryExpenseBill.getWaterExpense();
        Double electricityExpense = dormitoryExpenseBill.getElectricityExpense();
        // 创建数据时，参数不能为空
        if (add) {
            // todo 补充校验规则
            ThrowUtils.throwIf(dormitoryId==null, ErrorCode.PARAMS_ERROR);
            ThrowUtils.throwIf(waterExpense==null, ErrorCode.PARAMS_ERROR);
            ThrowUtils.throwIf(electricityExpense==null, ErrorCode.PARAMS_ERROR);
        }
        // 修改数据时，有参数则校验
        // todo 补充校验规则
    }

    /**
     * 获取查询条件
     *
     * @param dormitoryExpenseBillQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<DormitoryExpenseBill> getQueryWrapper(DormitoryExpenseBillQueryRequest dormitoryExpenseBillQueryRequest) {
        QueryWrapper<DormitoryExpenseBill> queryWrapper = new QueryWrapper<>();
        if (dormitoryExpenseBillQueryRequest == null) {
            return queryWrapper;
        }
        // todo 从对象中取值
        Integer billId = dormitoryExpenseBillQueryRequest.getBillId();
        Integer dormitoryId = dormitoryExpenseBillQueryRequest.getDormitoryId();
        Double waterExpense = dormitoryExpenseBillQueryRequest.getWaterExpense();
        Date billDate = dormitoryExpenseBillQueryRequest.getBillDate();
        Double electricityExpense = dormitoryExpenseBillQueryRequest.getElectricityExpense();
        String searchText = dormitoryExpenseBillQueryRequest.getSearchText();
        String sortField = dormitoryExpenseBillQueryRequest.getSortField();
        String sortOrder = dormitoryExpenseBillQueryRequest.getSortOrder();
        if(sortField==null){
            sortField="billDate";
            sortOrder=CommonConstant.SORT_ORDER_DESC;
        }
        // todo 补充需要的查询条件
        // 从多字段中搜索
        if (StringUtils.isNotBlank(searchText)) {
            // 需要拼接查询条件
            queryWrapper.and(qw -> qw.like("title", searchText).or().like("content", searchText));
        }
        // 模糊查询
        // JSON 数组查询
        // 精确查询
        queryWrapper.eq(ObjectUtils.isNotEmpty( billId), "billId", billId);
        queryWrapper.eq(ObjectUtils.isNotEmpty(billDate),"BillDate",billDate);
        queryWrapper.eq(ObjectUtils.isNotEmpty( dormitoryId), "dormitoryId", dormitoryId);
        queryWrapper.eq(ObjectUtils.isNotEmpty( waterExpense), "waterExpense", waterExpense);
        queryWrapper.eq(ObjectUtils.isNotEmpty( electricityExpense), "electricityExpense", electricityExpense);
        // 排序规则
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }

    /**
     * 获取宿舍水电支出封装
     *
     * @param dormitoryExpenseBill
     * @param request
     * @return
     */
    @Override
    public DormitoryExpenseBillVO getDormitoryExpenseBillVO(DormitoryExpenseBill dormitoryExpenseBill, HttpServletRequest request) {
        // 对象转封装类
        DormitoryExpenseBillVO dormitoryExpenseBillVO = DormitoryExpenseBillVO.objToVo(dormitoryExpenseBill);
        // todo 可以根据需要为封装对象补充值，不需要的内容可以删除
        // region 可选
        // 1. 关联查询用户信息
        // 2. 已登录，获取用户点赞、收藏状态
        Integer billId = dormitoryExpenseBill.getBillId();
        QueryWrapper<Dormitory> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("dormitory_id",dormitoryExpenseBill.getDormitoryId());
        Dormitory dormitory = dormitoryService.getOne(queryWrapper);
        DormitoryVO dormitoryVO = dormitoryService.getDormitoryVO(dormitory, request);
        dormitoryExpenseBillVO.setDormitoryVO(dormitoryVO);
        // endregion
        return dormitoryExpenseBillVO;
    }

    /**
     * 分页获取宿舍水电支出封装
     *
     * @param dormitoryExpenseBillPage
     * @param request
     * @return
     */
    @Override
    public Page<DormitoryExpenseBillVO> getDormitoryExpenseBillVOPage(Page<DormitoryExpenseBill> dormitoryExpenseBillPage, HttpServletRequest request) {
        List<DormitoryExpenseBill> dormitoryExpenseBillList = dormitoryExpenseBillPage.getRecords();
        Page<DormitoryExpenseBillVO> dormitoryExpenseBillVOPage = new Page<>(dormitoryExpenseBillPage.getCurrent(), dormitoryExpenseBillPage.getSize(), dormitoryExpenseBillPage.getTotal());
        if (CollUtil.isEmpty(dormitoryExpenseBillList)) {
            return dormitoryExpenseBillVOPage;
        }
        // 对象列表 => 封装对象列表
        List<DormitoryExpenseBillVO> dormitoryExpenseBillVOList = dormitoryExpenseBillList.stream().map(dormitoryExpenseBill -> {
            return DormitoryExpenseBillVO.objToVo(dormitoryExpenseBill);
        }).collect(Collectors.toList());

        // todo 可以根据需要为封装对象补充值，不需要的内容可以删除
        // region 可选
        // 1. 关联查询用户信息
        // 2. 已登录，获取用户点赞、收藏状态
        // 填充信息
        dormitoryExpenseBillVOList.forEach(dormitoryExpenseBillVO -> {
            Integer billId = dormitoryExpenseBillVO.getBillId();
            QueryWrapper<Dormitory> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("dormitory_id",dormitoryExpenseBillVO.getDormitoryId());
            Dormitory dormitory = dormitoryService.getOne(queryWrapper);
            DormitoryVO dormitoryVO = dormitoryService.getDormitoryVO(dormitory, request);
            dormitoryExpenseBillVO.setDormitoryVO(dormitoryVO);
        });
        // endregion
        dormitoryExpenseBillVOPage.setRecords(dormitoryExpenseBillVOList);
        return dormitoryExpenseBillVOPage;
    }

    @Override
    public List<DormitoryExpenseBill> getRecentBill(Integer dormitoryId) {
        return dormitoryExpenseBillMapper.getRecentBill(dormitoryId);
    }

}
